// pages/login/index.js
var app=getApp();
import WxValidate from '../../utils/WxValidate';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    relogin: app.globalData.relogin
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  showModal(error) {
    wx.showToast({
      title: error.msg,
      duration: 2000,
      icon: 'none'
    });
  },
  initValidate() {
    const rules = {      
      mobile: {
        required: true,
        tel: true,
      },
      password: {
        required: true,
        minlength: 6,
        maxlength: 16
      }
    }
    const message = {     
      mobile: {
        required: '请输入11位手机号码',
        tel: '手机号不能少于和大于11位',
      },      
      password: {
        required: '请输入密码',
        minlength: '密码长度不能少于6位',
        maxlength: '密码长度不能超过16位'
      },
    }
    this.WxValidate = new WxValidate(rules, message);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      let params =  wx.getStorageSync('params')
      let phone = '',pas = '';
      if(params){
          let data = JSON.parse(params)
          phone = data.mobile
          pas = data.password
      }
      var relogin=app.globalData.relogin;
      if(relogin){
        this.showMessage("您的帐号在其他设备上登录请确认！");
        console.log("您的帐号在其他设备上登录请确认！");
      }
      this.setData({
          phone: phone,
          pas:pas
      })
  },
  showMessage:function(message){
    wx.showModal({
      title: '登录提示',
      confirmText:'确定',
      cancelText:'取消',
      content: message,
      success:function(res){
       
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 注册点击事件
   */
  registorpage: function () {
    wx.navigateTo({
      url: '../registorpage/index',
    })
  },
  submitMethod:function(events){
    const params = events.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error);
      return;
    }
    params.openid = app.data.openid;
    console.log(params)
    debugger
    //这里相当于扫码进入，第一次进来，没有openid
    if(!app.data.openid){

      wx.login({
        success: res => {
          var code = res.code;

          var sumit = {
            appid: 'wxd598d704f54aa600',
            secret: '09ff4e7f657eda6eecb4041d77fc58af',
            js_code: res.code
          };
          app.paiRequest("/app/user/login", sumit).then(function(res) {
            var data = res.data;
            getApp().globalData.userInfo = data;
            getApp().data.openid = data.wxOpenid;
            _this.setData({
              userInfo: data
            })
            var systeminfo = data.systeminfo;
            if (systeminfo > 0) {
              _this.playMp();
            }
            if (_this.data.socketStatus === 'closed') {
              _this.openSocket(data.id);
            }
          })

        }
      })
    }else{
      app.paiRequest("/app/user/login", params).then(function (res) {
        app.globalData.userInfo = res.data;
        app.globalData.relogin=false;
        wx.switchTab({
          url: '/pages/index/index',
        })
        wx.setStorageSync('params', JSON.stringify(params))
      })
    }





  },
  
  /**
   * 登录事件
   */
  btnclick:function(events){
      console.log(events);
  },
  bindGetUserInfo: function (e) {
    var userInfo = e.detail.userInfo;
    var src = '../registorpage/index?avatarUrl=' + userInfo.avatarUrl + '&gender=' + userInfo.gender + '&sign=' + userInfo.nickName
    wx.navigateTo({
      url: src,
    })
    
  },
  mobilenum:function(e){
     var mobile=e.detail.value;
     this.setData({
       mobile:mobile
     })
  },
  selepass:function(e){
    var mobile=this.data.mobile;
    if (mobile==undefined){
      wx.showToast({
        title: '请先输入手机号',
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }else{
      app.paiRequest('/app/user/selectQuetions', {mobile: mobile}).then(function (res) {
        var questions = res.data;
        questions.mobile = mobile;
        var parameters = JSON.stringify(questions);
        wx.navigateTo({
          url: 'selepass/index?questions=' + parameters,
        })
         
      });
      
    }
  }
})